package com.vin.redis.examples;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.StandardCharsets;

/**
 * <br>
 *
 * @author levin
 * @version 1.0.0
 */
//@Configuration
public class    Config {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer1 = new Jackson2JsonRedisSerializer(Object.class);
       // FastJsonRedisSerializer genericJackson2JsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
        ObjectMapper om = new ObjectMapper(); //JackSon对象
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        ObjectMapper om1 = new ObjectMapper(); //JackSon对象
        om1.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //om1.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer1.setObjectMapper(om1);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer1);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    public static void main(String[] args) {
      /*  FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(null);

        String key = "abc";
        print(key.getBytes(StandardCharsets.UTF_8));

        byte[] abcs = fastJsonRedisSerializer.serialize("abc");
        print(abcs);

        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        abcs = genericJackson2JsonRedisSerializer.serialize("abc");
        print(abcs);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        abcs = stringRedisSerializer.serialize("abc");
        print(abcs);*/
            System.out.println("/../../etc/passwd&fileName=%E5%AE%A2%E6%88%B7%E6%B5%81%E6%B0%B4".contains("../"));

    }

    static void print(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");
        }
        System.out.println();
    }
}
